package com.shop.shoppingmall.service.impl;

import com.shop.shoppingmall.dao.ShFollowShopDao;
import com.shop.shoppingmall.dao.ShUserDao;
import com.shop.shoppingmall.entity.ShFollowShop;
import com.shop.shoppingmall.entity.ShMerchant;
import com.shop.shoppingmall.entity.ShUser;
import com.shop.shoppingmall.service.ShFollowShopService;
import com.shop.shoppingmall.util.CommonConstant;
import com.shop.shoppingmall.util.JwtUtil;
import com.shop.shoppingmall.util.UUIDGenerator;
import com.shop.shoppingmall.util.api.vo.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 店铺关注(ShFollowShop)表服务实现类
 *
 * @author makejava
 * @since 2020-09-09 10:06:18
 */
@Service("shFollowShopService")
@Transactional
public class ShFollowShopServiceImpl implements ShFollowShopService {
    @Resource
    private ShFollowShopDao shFollowShopDao;
    @Resource
    private ShUserDao shUserDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ShFollowShop queryById(String id) {
        return this.shFollowShopDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ShFollowShop> queryAllByLimit(int offset, int limit) {
        return this.shFollowShopDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param shFollowShop 实例对象
     * @return 实例对象
     */
    @Override
    public ShFollowShop insert(ShFollowShop shFollowShop) {
        this.shFollowShopDao.insert(shFollowShop);
        return shFollowShop;
    }

    /**
     * 修改数据
     *
     * @param shFollowShop 实例对象
     * @return 实例对象
     */
    @Override
    public ShFollowShop update(ShFollowShop shFollowShop) {
        this.shFollowShopDao.update(shFollowShop);
        return this.queryById(shFollowShop.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.shFollowShopDao.deleteById(id) > 0;
    }

    /**
     * 关注商铺
     * @param
     * @param merchantId
     * @return
     */
    @Override
    public Result addFollowShop(HttpServletRequest request, String merchantId) {
        String nameByToken = JwtUtil.getUserNameByToken(request);
        ShUser shUser = shUserDao.selectLoginName(nameByToken);
        if(shUser == null){
            return Result.error(CommonConstant.SC_OK_201,"您还未注册或者登录，请先注册或登录");
        }
        String userId = shUser.getId();
        //查询实体信息
        ShFollowShop followShop = shFollowShopDao.selectFollowShop(userId, merchantId);
        if(followShop == null){
            //自动生成id
            String id = UUIDGenerator.generate();
            ShFollowShop shFollowShop = new ShFollowShop();
            shFollowShop.setId(id);
            shFollowShop.setUserId(userId);
            shFollowShop.setMerchantId(merchantId);
            shFollowShopDao.insert(shFollowShop);
            return Result.ok("关注成功！");
        }else {
            return Result.ok("您已经关注了该店铺!");
        }
    }

    /**
     * 取消关注商铺
     * @param
     * @param merchantId
     * @return
     */
    @Override
    public Result deleteFollowShop(HttpServletRequest request, String merchantId) {
        String nameByToken = JwtUtil.getUserNameByToken(request);
        ShUser shUser = shUserDao.selectLoginName(nameByToken);
        if(shUser == null){
            return Result.error(CommonConstant.SC_OK_201,"您还未注册或者登录，请先注册或登录");
        }
        String userId = shUser.getId();
        ShFollowShop followShop = shFollowShopDao.selectFollowShop(userId, merchantId);
        if(followShop == null){
            return Result.error("您还未关注该店铺");
        }
        shFollowShopDao.deleteFollowShop(userId, merchantId);
        return Result.ok("取消成功!");
    }

    /**
     * 查询该用户对应的所有店铺
     * @param
     * @return
     */
    @Override
    public Result selectAllFollowShop(HttpServletRequest request) {
        String nameByToken = JwtUtil.getUserNameByToken(request);
        ShUser shUser = shUserDao.selectLoginName(nameByToken);
        if(shUser == null){
            return Result.error(CommonConstant.SC_OK_201,"您还未注册或者登录，请先注册或登录");
        }
        String userId = shUser.getId();
        List<ShMerchant> shFollowShops = shFollowShopDao.selectAllFollowShop(userId);
        if(shFollowShops.size() == 0){
            return Result.error("您还未关注任何商铺");
        }
        return Result.ok(shFollowShops);
    }
}